package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询围栏:geofenceQuery={}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        //之前在围栏管理页只查询了围栏名称/坐标/创建时间/状态,并没有对车辆信息进行处理
        //现在需要给围栏补充车辆总数与可用车辆数的数据
        for (int i = 0; i < list.size(); i++) {
            //依次取出每个遍历到的围栏对象
            GeofenceVO geofenceVO = list.get(i);
            //查询当前围栏上绑定的所有车辆
            VehicleQuery vehicleQuery = new VehicleQuery();
            //设置车辆的查询条件为:当前遍历到的围栏id
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //获取当前围栏的绑定车辆总数
            int totalNum = vehicleVOList.size();
            //再定义变量保存可用车辆数
            int availableNum = 0;
            for(VehicleVO vehicleVO : vehicleVOList){
                //车辆状态 1空闲 2占用 3维修...
                if(vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            //给当前围栏对象设置可用车辆数,如果可用车辆数>0,则说明有可用车辆,否则车用车辆数为0
            if(availableNum>0){
                geofenceVO.setAvailableNum(availableNum);
            }else{
                geofenceVO.setAvailableNum(0);
            }
            //给围栏对象设置车辆总数
            geofenceVO.setTotalNum(totalNum);
            //后续进一步查看车辆详情时会使用车辆数据,所以一起存入geofenceVO返回给前端
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam, geofence);
        //前端传过来的数据是K-V结构
        //{type:'circle', longitude: 116, latitude: 39, radius: 661}
        //{type:'rectangle', recPoints: '116-39,116-39,116-39,116-39'}
        HashMap<String, String> position = geofenceParam.getPosition();
        //准备JSON处理库工具
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //这个方法如果传入null值或者字段不匹配会抛出异常,所以放置于try-catch中来处理即可
            String positionStr = objectMapper.writeValueAsString(position);
            if (geofenceParam.getId() != null) {//更新围栏
                geofenceMapper.update(geofence);
            } else {//新增围栏
                geofence.setStatus("1");//启用状态
                geofence.setPosition(positionStr);
                geofence.setCreateTime(new Date());
                geofenceMapper.insert(geofence);
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void deleteGeofence(Long id) {
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        if (list != null && list.size() > 0) {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        } else {
            geofenceMapper.deleteById(id);
        }
    }
}
